<?php
namespace Game\Controller;
use Common\Controller\AdminbaseController;
use Library\GameTable;
class DesktopController extends AdminbaseController {
	protected $game_desktop_model;
	function _initialize() {
		parent::_initialize();
		$this->game_desktop_model = D("GameDesktop");
	}

    private function materialstatus() {
		$materialstatuss =
		array(
			array(
	            array('id' => '0','name'=>'请选择'),
	            array('id' => '1','name'=>'视频'),
	            array('id' => '2','name'=>'图片'),
	            array('id' => '3','name'=>'游戏'),
	        ),
	        array(
	            array('id' => '0', 'name'=>'请选择'),
	            array('id' => '1', 'name'=>'未启用'),
	            array('id' => '2', 'name'=>'已启用'),
	            array('id' => '3', 'name'=>'已停用'),
	            array('id' => '4', 'name'=>'上传错误'),
	        ),
		);
		return $materialstatuss;
    }
    function index(){
        if(I('param.start_time')){
                $search[] = array('start_at'=>array('egt',I('param.start_time')));
        }
        if(I('param.end_time')){
                $search[] = array('end_at'=>array('elt',I('param.end_time')));
        }
        if(I('param.dstatus')){
                $search[] = array('dstatus'=>I('param.dstatus'));
        }
        if(I('param.dtype')){
                $search[] = array('dtype'=>I('param.dtype'));
        }
		$_GET['start_time'] =I('param.start_time');
		$_GET['end_time'] =I('param.end_time');
		$_GET['dstatus'] =I('param.dstatus');
		$_GET['dtype'] =I('param.dtype');
		$mstatus = $this->materialstatus();
		$count=$this->game_desktop_model
		->where($search)
		->count();
		$page = $this->page($count, 50);
		$game = $this->game_desktop_model
		->where($search)
		->limit($page->firstRow . ',' . $page->listRows)
		->order("id DESC")
		->select();
		if($game){
			foreach($game as $guser){
				$uid[] =$guser['userid'];
			}
			$users=M("users")->field("id,user_login")->where(array('id'=>array('in',$uid)))->select();
			$this->assign("users", $users);
		}
		$this->assign("urlimg", C('IMG_PATH'));
		$this->assign("status", $mstatus[1]);
		$this->assign("type", $mstatus[0]);
		$this->assign("game", $game);
		$this->assign("page", $page->show('Admin'));
		$this->assign("count",$count);
		$this->display();
	}
	function add(){
		$mstatus = $this->materialstatus();
		$this->assign("type", $mstatus[0]);
		$this->display();
	}
	function add_post(){
		if (IS_POST) {
			if(!I('post.name')){
				$this->error('请输入素材名称');
			}
			if(!I('post.dtype')){
				$this->error('请选择素材类型');
			}
			$_POST['create_user'] = get_current_admin_id();
			$_POST['userid'] = get_current_admin_id();
			if($_POST['update_file']){
				$_POST['dstatus'] = 0;
				$Aliyun = new \Aliyun\Controller\AliyunController();
				$local_file = $Aliyun->save_file($_POST['update_file'],'img/'.CONTROLLER_NAME);
				$_POST['local_file'] = $local_file['path'].$local_file['name'];
				if($_FILES["update_img"]){
					$update_img = $Aliyun->save($_FILES["update_img"],'img/'.CONTROLLER_NAME);
					$_POST['local_img'] =$update_img['path'].$update_img['name'];
				}
			}else{
				$_POST['dstatus'] = 1;
			}
			$did = $this->game_desktop_model->add($_POST);
			if($did){
				if($_POST['local_file']){
					$this->success("后台数据处理中...",U('Desktop/index'),0,'AJSON');
					$result = GameTable::addSource($_POST['local_file'],$_POST['local_img']);
					if(is_string($result)){
						$des['remote_error'] = $result;
						$des['dstatus'] = 4;
					}else{
						$des['remote_error'] = '';
						$des['dstatus'] = 1;
					}
					$des['remote_file'] = $result[0]['serverPath'];
					$des['remote_img'] = $result[0]['serverPath'];
					$des['return_json'] = json_encode($result);
					$this->game_desktop_model->where(array('id'=>$did))->save($des);
				}else{
					$this->success("添加成功！",U('Desktop/index'));
				}
			} else {
				$this->error("添加失败！");
			}
		}
	}
	function edit(){
		$id = array(intval(I("get.id")));
		$game = $this->game_desktop_model->where(array('id'=>$id))->find();
		$this->assign("urlimg", C('IMG_PATH'));
		$mstatus = $this->materialstatus();
		$this->assign("type", $mstatus[0]);
		$this->assign("game", $game);
		$this->display();
	}
	function edit_post(){
		if (IS_POST) {
			$_POST['userid'] = get_current_admin_id();
			if($_POST['update_file'] || $_POST['local_file']){
				$Aliyun = new \Aliyun\Controller\AliyunController();
				if($_POST['update_file']){
					$local_file = $Aliyun->save_file($_POST['update_file'],'img/'.CONTROLLER_NAME);
					$_POST['local_file'] =$local_file['path'].$local_file['name'];
				}
				if($_POST['local_file'] && $_FILES["update_img"]){
					$update_img = $Aliyun->save($_FILES["update_img"],'img/'.CONTROLLER_NAME);
					$_POST['local_img'] =$update_img['path'].$update_img['name'];
				}
				$_POST['dstatus'] = 0;
			}
			if($this->game_desktop_model->where(array('id'=>I("post.id")))->save($_POST)){
				if($_POST['local_file']){
					$this->success("后台数据处理中...",U('Desktop/index'),0,'AJSON');
					$result = GameTable::addSource($_POST['local_file'],$_POST['local_img']);
					if(is_string($result)){
						$des['remote_error'] = $result;
						$des['dstatus'] = 4;
					}else{
						$des['remote_error'] = '';
						$des['dstatus'] = 1;
					}
					$des['remote_file'] = $result[0]['serverPath'];
					$des['remote_img'] = $result[0]['serverPath'];
					$des['return_json'] = json_encode($result);
					$des['dstatus'] = 1;
					$this->game_desktop_model->where(array('id'=>I("post.id")))->save($des);
				}else{
					$this->success("修改成功！",U('Desktop/index'));
				}
			} else {
				$this->error("修改失败！");
			}
		}
	}
	public function up(){
    	if (I("get.id")) {
    		if ($this->game_desktop_model->where(array('id'=>I("get.id")))->setField('dstatus','2')) {
    			$this->success("素材启用成功！");
    		} else {
    			$this->error('素材启用失败！');
    		}
    	} else {
			$this->error($this->game_desktop_model->getError());
    	}
    }

    public function down(){
    	if (I("get.id")) {
    		if ($this->game_desktop_model->where(array('id'=>I("get.id")))->setField('dstatus','3')) {
    			$this->success("素材停用成功！");
    		} else {
    			$this->error('素材停用失败！');
    		}
    	} else {
			$this->error($this->game_desktop_model->getError());
    	}
    }
	public function delete(){
    	if (I("get.id")) {
    		$desktop = $this->game_desktop_model->where(array('id'=>I("get.id")))->find();
    		if ($desktop) {
					$Aliyun = new \Aliyun\Controller\AliyunController();
				if($desktop['local_img']){
					$Aliyun->delete($desktop['local_img']);
				}
				if($desktop['local_file']){
					$Aliyun->delete($desktop['local_file']);
				}
    			$desktop = $this->game_desktop_model->where(array('id'=>I("get.id")))->delete();
    			$this->success("素材删除成功！");
    		} else {
    			$this->error('素材删除失败！');
    		}
    	} else {
			$this->error($this->game_desktop_model->getError());
    	}
    }
}
